#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<vector>

std::string ToHex(void* addr)
{
    char addrbuffer[64];
    snprintf(addrbuffer,64,"%p",addr);
    return addrbuffer;
}

int gval=0;

void* start_routine(void* args)
{
    std::string name=static_cast<const char*>(args);
    while(1)
    {
        std::cout<<"我是新线程，"<<pthread_self()<<",threadname is : "<<name<<",gval: "<<gval<<", "<<"&gval: "<<ToHex(&gval)<<std::endl;
        gval++;
        sleep(1);
    }
    sleep(2);
    return (void*)11;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,start_routine,(void*)"thread-1");
    sleep(2);

    while(1)
    {
        sleep(1);
        std::cout<<"我是主线程，"<<pthread_self()<<",threadname is : main"<<",gval: "<<gval<<", "<<"&gval: "<<ToHex(&gval)<<std::endl;

    }
    void* ret=nullptr;
    int n=pthread_join(tid,&ret);
    (void)n;
    return 0;
}